package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.util.UserInfoUtils;

public class StartUpActivity extends AppCompatActivity {
    private long initTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_up);
        initTime = System.currentTimeMillis();
        gotoMain();

    }
    private synchronized void gotoMain(){

        new Thread(){
            @Override
            public void run() {
                long endTime = 1000 - (System.currentTimeMillis() - initTime);
                if(endTime > 0){
                    try {
                        sleep(endTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        boolean isLogin = UserInfoUtils.getIsLogged();
                        if (isLogin) {
                            Intent intent = new Intent(StartUpActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Intent intent = new Intent(StartUpActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            }
        }.start();
    }
}
